package com.proyecto.robomind.game;

import com.proyecto.robomind.R;
import com.proyecto.robomind.interpreter.Interpreter;
import com.proyecto.robomind.map.MapHandler;
import com.proyecto.robomind.model.MapSquare;
import com.proyecto.robomind.model.Ninja;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback{

	private static final String TAG = MainGamePanel.class.getSimpleName();
	
	private MainThread thread;
	private Interpreter interp;
	private Ninja ninja;
	private MapSquare[][] currentMap;
	private String mapTitle;
	private int height, width, heightCount, widthCount;
	private String avgFps;
	
	public MainGamePanel(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		getHolder().addCallback(this);

		thread = new MainThread(getHolder(),this);
		setFocusable(true);
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		thread.setRunning(false);
		thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean retry = false;
		while(retry){
			try{
				thread.join();
				retry = true;
			}catch(InterruptedException e){
				
			}
		}	
	}
	
	public void setScript(String script){
		interp.setScript(script);
	}
	
	public void loadMap(MapSquare[][] currentMap,String mapTitle){
		this.currentMap = currentMap;
		this.mapTitle = mapTitle;
		heightCount = currentMap.length;
		this.widthCount = currentMap[0].length;
		loadNinja(MapHandler.atOrigin(currentMap));
		if(!thread.isRunning()){
			Canvas canvas = getHolder().lockCanvas();
			render(canvas);
			getHolder().unlockCanvasAndPost(canvas);
		}
	}
	
	public void loadNinja(MapSquare map){

		//create droid and load bitmap
		if(map!=null){
			ninja = new Ninja(BitmapFactory.decodeResource(getResources(), R.drawable.ninja_spritesheet),
				map.getX()+(map.getWidth()/4),map.getY()+(map.getHeight()/4),height/10, width/10,5,3);
			interp = new Interpreter(ninja, currentMap);
		}
	}
	
	public void setAvgFps(String avgFps){
		this.avgFps = avgFps;
	}
	
	protected void render(Canvas canvas){
		canvas.drawColor(Color.GRAY);
		if(currentMap != null){
			for(int i = 0; i < heightCount; i++){
				for(int c = 0; c < widthCount; c++){
					currentMap[i][c].draw(canvas);
				}
			}
		}
		ninja.draw(canvas);
		displayFpsTitle(canvas, avgFps,mapTitle);
	}
	
	private void displayFpsTitle(Canvas canvas, String fps,String title){
		if(canvas != null && fps!=null){
			Paint paint = new Paint();
			paint.setARGB(255,0,0,0);
			canvas.drawText(fps, this.getWidth()-50,20,paint);
			canvas.drawText(title, 50 ,20,paint);
		}
	}
	
	public void update() {
		ninja.update(System.currentTimeMillis());
	}
	
	public void setThreadRunning(boolean running){
		thread.setRunning(running);
		interp.setThreadRunning(running);
	}
}
